fixed few bugs in substitute.