Refactored code into subfunction to re-use