Large refactoring for #76.

Did a lot of cleanup, squashed commits (to get rid of some ugly
version history), and implemented tow's requests.