Refactoring of filtering code