Fix issue #538 and add test
Merge