improved error handling and stability