Backported commits 0e50ecb525003b87e8199708008e4a18606c5e54 and 570e12942ba3d2363ec7098dcf1d35c6b68667a1 to 2.0.