Fixed bug in substitution code