finished the logic for matching.