Unit tests and refactorings