improved mask creation