Reorganized the javascript a bit