Updated the code according to the comments