added comments to describe what the function does