Added comments for each section of the function