Refactor API. Cleanup. Add test.