More tests and some refactoring.