refactor code and tests