Adding ApiError handling. Rewritten some tests.