improved mask logic