Clean up filtering code