Improve logging and exception handling.