remove unused variables, and improved substitution regular expression.