Refactoring JavaScript