fix mesh algorithm to handle more segmentation cases