Added some comments to make this easier to review.