Cherry picking lost important changes from 7410cd0b4c711bca8a395070d46ddbc49fba31dd