Building filter algorithm