Add more consistent error handling and flow control.