Fixed error in variable-substitution loop
Corrected some variable names.