Code refactoring: Extracted repeating code to function.