improve download function