improved the card matching algorithm