Fixed memory allocation bug