Tidying code up a bit, adding comments.