New comments added to clarify the code