fix: move some code to function, add logger
fix: move some code to function, add logger