Fix tracking of token end. Refactor some references