Backport. Fixes #1186.