refactoring  javascript