refactor matches function