refactor the javascript