Fixing memory allocation