Back port the changes from 40aa1084bada1af63078f624cd94d7b6fd26f013