Added refactored code. Hope this helps.

Hi, this was quite fun to do. Please let me know if you have any questions. I fixed some stuff as I've noticed that you are using some Ruby syntax for some JS functions.