refactor the js code