updated and refactored code