Some refactoring, and more tests