Tidied up client-side JS