made some of the javascript a little more DRY