Move mapping-iteration logic out of unmap() and into reusable helper.

#926 part 1.  Semantics-preserving refactoring.