Simplify some code
Add more tests for api