Building sample filtering algorithm