Tidying up JS

Making more variables and refining JS code