faster algorithm when removing matches