extract common mask building function