fix bugs in substitute token; minor code twist