some refactor code and add test