Updated logic to allow duplicate pairs