refactored & updated the code