refactored code further