Manually merge #5978. Tests passed locally.