refactored code working