refactor code and complete test