Added check for valid terminator span after replacement