Refactored javascript, pulling it out into its own class and cleaning it up.