Fix for issue #8519

Also added a test that fails prior to the fix, illustrating the problem.