implemented function to free memory for tokens