refactor code and test