Finish api, refactor tests, add tests for API.