Split tokenize loop into its own function