Refactored code with non-borrowing versions.