refactored the code based on PR feedback