First off, apologies for not answering sooner, but my internet has been down :'(
I can fix this whole problem for you and it would also minimise your script, by using containers. Now if you want to have a play with them, and do it yourself, be my guest (as they are a super handy thing to learn and understand) if you just want it to work, message back and I'll just do it for you.