Improve filtering and add some tests.