Thank you very much for all the help StormCrow42, I've fixed the last of the problems.
To track down these issues I changed the script into a recovery simulator that enabled me to see how it would perform under various conditions without actually restoring anything. I even found a couple of minor issues that you didn't find yet.

Thank goodness I thought to do that.
I'm
quite certain that it will work properly now. I'm embarrassed that it was released in such a buggy condition, but pleased that it will now use magical mystery juice and plump juicy grubs if it can do so.
V2.03 is now released.