move more duplicated code to utils

also updated tests