correct matches function algorithm