Merge "Fix potential segfaults and double free corruption"