Minor optimizations and cleanups

Clarified some of the comments, tweaked a few small things, and changed
the formatting for the output. Still the same algorithm as before!