Refactor filterFn to make loop() function cleaner