implemented best match logic