This is a backport of part of !3598 (merged) from master without all of the refactorings.
BUG: 466299