implemented most of the filtering