refactoring total token count loop