Moving auto-match logic to database.