Moved download code to an independent function