Safer and more efficient string copying, and corrected some pointer bugs