Better matching algorithm