Manual cherrypick for MR 46724
This MR is a manual cherry-pick of the changes in !46724 (merged) (merged) to fix ATEAM-780 in 21.0-mc16a. But it seems the two branches (master and 21.0-mc16a) diverged quite a lot and there were conflicts. I tried to resolve them but there are much more changes in this MR as in the original MR !46724 (merged) (merged). @ssnyder , can you please take a loo whether this MR is ok?