Clean up some code to prevent seg faults