Fixes #4649

Redo of the previous bad PR. Good thing for git cherry-pick.