Bring a lot of merged code from CM source that didn't patch properly with cherry-picks.