backport of 7ab8652b2fd9d48af01a75cf3742c9d3085c398c from trunk, closes #655