backporting fix from 00b39e0145c5b9117503b20bba74948294a30e5a