some refactoring to improve organization of javascript