Refactor the code and add some documentation to prepare for a review.