Building the filtering algorithm