Fix duplicate matching in some cases