Refactoring protocols and adding unit tests.