Tidying up code, adding some new comments