restructuring js code