Added comments to explain code logic