Major restructuring.

Basic happy-path tests are passing.