Fixed a severe issue with item matching

The code matched items in the wrong way causing deletion of items that
should have been kept