refactored main js script a bit, mostly clean up