Extract buildMatchingMask method