cleanup mapping code

There was quite a bit of duplicated logic, which wasn't duplicated where
it needed to be.

Make functions out of the duplicated bits, then call where appropriate.

Issue: #56