in reply to error in script
Can you reproduce the error in ten lines of code? (Doing so will probably take some work, and that process will probably lead you to discovering what the problem is.)
Dave