Tidied up and added a few comments