some refactoring for more generic filtering