reorged the js code