Backport bug fix. Fixes bug#1240.