Refactoring download function