replace asserts with return of various error codes