Small refactoring of download function