Fix a bug when downloading last piece, add error handling