Created by: tsenart
I intend to extract multiple smaller PRs from this branch. Together, they resolve #6403.