Your post is not at all trivial. Sometimes the way that programs or scripts work is not immediately obvious, even to experienced programmers.

It is good to have these questions asked as it allows us to post a response that will help people with the same issue.