refactored into one single filter function that works for all