Improve filter handling code