Adding API tests.  Fixing bugs found by tests