restructuring the javascript a bit