add more comments to explain parts of the function