fixed memory allocation bug