Separated logic into functions.
The read_chunk and write_chunk are untested. As is the loop to read and
write the file.