Updated the solution based on code review