Move mask operations to MaskFilter function

Free up memory from mask operations at first opportunity.