Trying to tidy up the javascript