Improve return value checking and error handling.