Improved filter code