Improve logging and code refactoring.