Refactoring, add logging