More tests and a bit refactoring.