You just regurgitated your code instead of rewriting your entry conditions in a logical paragraph form, which I would have preferred. I say this because your description is possibly wrong, and I have no way to confirm this.

Anyways, if you set Verbose = 7 | DIAG, and set(LOGFILE), add various printf statements, and then read the logs in your log folder, you will be able to troubleshoot your script.

If you're still having trouble, post your **entire** script and not little snippets.