Changed file writing to only happen on close and fixed a small error
when downloading