implementing tests, refactored and improved filtering logic