it might be cleaner to do the match only once beforehand, and build a function to be used here.