Tidied up the code a bit