Updated the code with comments