Some cleanup and restructure of the js file