Added comments to each line of code for explanation.