refactor code and test.