backport of f3372bac91eafceafaa3da516a861d06b7246297 from trunk, closes #675