Restructuring the javascript to be a bit more modular