Improve logging code