move code to find matching tokens to generic function