Faster implementation of filter