fix: move some code to function, add logger