tidied up, added comments