Another round of refactoring and touchup.