add some function and refactor code

add some function and refactor code