Some refactoring and improved log