Fix segmentation errors