API error handling
Unfinished further refactoring