Wip 8269

A fix for #8269, and an optimization on top of it.