refactored code for functionality