some js refactoring