Refactoring javascript