Refactored code to pass by reference