backport of 24ee4e0dd729df63873d2e0e22de2aa6e57c8401 from trunk, closes #579