added the code with appropriate comments