When results are still wrong you have probably more bugs in your script. I'm not this good in script reading but If you cannot solve the mystery here, you can always get a support ticket and ask support. There are also valuable hints here: https://manual.zorro-project.com/trouble.htm