Fixed substitution.

Creating pull request for @traiansf 's code, but rebased onto correct history. I will review this myself since it's his code.