Fix formatting, add comments, and clean up code.