Improve error handling and consistency checks.