Add testing and refactor api to be more unit testable.