added more comments to explain the code