Refactoring javascripts