javascript refactoring: