As with all software, most of the code is for error handling and exceptions....
Here is a new version I have tried as much as possible, seems to handle all situations I can imagine
If you look in the code you could enable a print statement that will help if you run into some problems. Otherwise I think the best is to add print statements here and there to print out variable values during design & debugging
Lets hit it and see if you find some more errors
Best regards, Walter