restructured js code