refactored the function to apply DRY