Final refactoring, all tests added.