Fixed memory allocation issues