Added comments to code to understand better.