refactored the filter() method for clarity.