implementation of the matching