FIX: improve matching algorithm