some javascript refactoring