Fix API tests

This PR mostly fixes the API tests. Some API code was changed also, but I'm sceptic about it. Most probably the API clients do not work after this change.

So, before merging this to master we should consider taking down the API.