That's because i didn't touch your code down there. ^^
Here, fixed that for you.
Please note that i didn't read myself through your code, so it's absolutely possible that there's still something messed up.
You should personaly know what you're doing.
If you can't keep up with your own pace, make sure to add comments to your own code so you understand what's going on, even 5 years later.
You can still put that bottom part together with what i wrote to make it smaller.
Here's a cleaner version: