First version of the matching algorithm