refactor code and new tests