refactored and upadated the source