Added comments based on my understanding of the code.