Slight refactoring and reorganization for readability.