Backport of fix from 1.5.1 branch:
Fixes #835