Significant refactoring and test writing.