Refactor downloadToFile