mild tidying up, added code comments