Fix #5501

Fixes issue #5501 and covers it with feature tests.