more refactoring and abstracting out of substitution regexp code