bug in substitution code. Need to fix it and then get type checking for whole program working