Add function documentation and error handling.