Improved error handling and documentation