Tests and minor refactoring