Refactor copyStream function adding functionality to abort transfer.