refactor and add logs