Fix some memory allocation issues