tidied up and commented