Fixes the filtering logic