Continue refactoring: Introduce tests, fix bugs...