It appears that the previous commit doesn't actually fix the segmentation fault because the code is isn't in the right place. I have commented out the code from the last commit as I haven't worked out where to put it yet.