I updated the script to correct a bug reported to me by a user named beni. The bug is mentioned in my description above.