tidied up code