Fixing possible memory allocation bugs