simplification in substitution and function evaluation
fixed wrong condition
minor refactoring
Merge pull request #1205 from andreistefanescu/master

simplification in substitution and function evaluation