Added comments to show algorithm better