fixed substitution bug and added tests