better logging and refactoring