Logging imporved and smaller refactoring.