move token-to-element code from state machine to tokens