Refactored the download logic