slice mask appropriately in flatten