Spruced up code

Made it more jshint friendly. Also, refactored quite a lot of code.