improved error handling and logging