Fix segmentation faults