add test and some code refactor
add test and some code refactor