Reverts the revert #119295 of #118882 by expanding #118882 with additional fixes which made CI unhappy.