Fixed several potential segmentation fault crashes