Fix segmentation faults and other small fixes