refactoring javascript