Tidied up the code slightly