More tests and other refactorings