Tidied up the JS a bit