reorganized main.js to a more logical work flow