ported d7712373433a4f234a822faa30b0cab022b6b2d5 from master