Fixed the segfault. Half finished work