refactoring a JS