More tests and refactoring.