restructure js code