changed the filter logic and did some refactoring