Possibly corrected file write

Changed write length algorithm