Added safety check when allocating strings