token flow logic