More tests and some resulting refactoring.