Refactor to make things simpler and fix build

This doesn't do much as far as real progress goes, but it sort of sets
the ground for it, considering I hadn't touched this in a while.