Refactored code for reusability