Issue 5527 namespace substs b

Separate out initial refactorings for PR #14604