Matching algorithm