Well, I would break that hundred lines of code further. Put the rest of the execution in a sub. When you popup a new window, do some stuffs, then you close it. On unload event of this popup window, execute the rest of the execution by calling that sub.