reorganized JS code