Added memory allocation and freeing.