Fixed a lot of jshint warnings and such, refactored.