improved error handling and debugging