Fixed API to return proper error codes