More cleanup and preparation for PR #9353

This commit is now depending on changes in PR #9353. Do not merge before that one is merged.