removing duplicates from the result, fixing a bug in the entity
filtering code