Started rewriting the substitution framework. Added test cases for #2.