Refactoring + add logging