refactored stream copy functionality