Also, made a few other refactoring changes for clarity. No functional changes intended. PiperOrigin-RevId: 414487729