Major refactoring. No functional change.