code tidied up for review