Clean up filtering code a bit