refactoring total token count loop

rewritting loop