fix bug with memory allocation