tidied up code a little