Backporting fix from d6eefa73e77632481c77ed2cd204940d56c1fca0