major clean up of javascript; resturctured as module