cleaned up filterEvents function