Tidied up code a bit