I updated the script to take your suggestions into account. I did not test it thoroughly though, because I'm in HCO, and resources are a little bit too scarce right now. I'll do more testing at the end of the run.
Unfortunately the script is still not working. I changed my way of doing, and...