Unit tests and necessary refactoring