Basic matching algorithm