Allocate and deallocate memory appropriately