Refactoring needed to get unit tests started.

I need to develop some unit tests, but there are some lower level functions that
would make unit testing easier. So I'm carefully trying to extract some
functions that should be pretty safe.