Tidied up, added comments.