More logging and refactor