Cleanup subst-gen

This is based on my previous PR and only the last 4 commits are unique to this.