Cherrypicked piece of ec31da2