Rebased on upstream (for iOS support)