Fix the bug with memory allocation