To on debug logging, run the script, when it's frozen, stop debugging logging. Hopefully it'll trace the path of the script and you can see what's going on. Without more info besides "it's freezing" it's difficult to look at the script and figure out what's going on (I did do a quick look-through). Maybe an infinite loop somewhere?