Refactored code to extract helper methods and to create a test setup fixture