Improve error handling and initialization