Fixes to substitution and some tests.