add more comments to make the function easy to understand