refactored filtering.cpp