Updated matching algorithm