move some code to separate function
add test