Fixed issues with memory allocation