tidied up code slightly