Finish writing download function