Further refactoring and test additions.