Error handling logic