Wholesale changes to API handling (api.cpp, api.hpp)