javascript refactoring