refactored code with test example