Added a mutex for memory allocation and deallocation