performance: move token_id out of 'for' loog