Cherrypicked bd65 and a82e