improve logging and error handling