Cherrypicked 7f0a and 98e7