Fixes #5953.

Long overdue fix for a regression I introduced.

Closes #5953. (Or should, it is very hard to test that specific case.)

Based on code from #6159.