Improve logging and refactor.