refactoring and cleaning up some of the JS