refactored download code