Backported #362 to 1.7.x branch

Fixes #360, backports #362.