Tidied up javascript a bit