tidied up javascript