refactoring the javascript