Improve download function