Refactored code into a subroutine