More unit tests; more refactoring.