Fixed null pointer errors.
Fixed an infinite loop on file write error.