Add Docstrings / Comments
Add clear docstrings and comments to the code below. Document what each function does, its parameters, and return value. Comment only the non-obvious logic — don't state the obvious. Keep the code itself unchanged. Code: [PASTE]