More tests and some minor refactorings