Refactored code into helper function