Refactored some of the javascript