Add comments, and simplify control flow in a couple of places for
readability.