move some code to separate function
add tests