More refactoring and tests.

None of this has been properly tested, of course.
Console!