Fixed really slow substitution bug