Reorganized js to minimize number of objects in global namespace