fixing memory allocation issues