Tidied up js code