Unit tests and related refactoring