Bug fix #8587

Fixes #8587; added a test for the issue.