javaScript refactoring