improve mask code, replace set with list