Refactor the filtering