Minor refactoring and refinements following merge from Willem's fork.