refactor main js code