old code with mask array, partially done