Fix a whole bunch of segfaults