Consolidating filtering code, fixing some bugs along the way