more logging and refactoring