Fix matching algorithm