If you place your original code and the code with my changes next to each other you will see the changes. I'm not going to list them one by one. All I did was implement some of my suggestions I posted earlier.

Your original code does not seem to have any bugs at first sight but I cannot guarantee that it will work immediately. You should always test your scripts in a small controlled environment before deploying them.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.