JavaScript refactoring