tidied up code a bit