added more comments to make the code understandable.