Tidied up and added comments