test: Teardown code was never called
Test cases for the API

Also API fixes to minor bugs found while running tests