Fixed up the JS to be more modular for building on top of