Modularised the javascript and tidied it up a little