Fix filtering algorithm