Fix and test for #237

Proposed fix for issue #237.
I also added a test case to test the fix.