Optimise javascript and general tidy up