Fix and test for #2607
Merge pull request #385 from etalanin:bug2607