refactored some js to be a bit more scalable