Add unit tests. More refactoring.