Marge code from 1df85964ee7e0e5701be0631754a3a0206e9fd6b in official master branch.