Fix some error handling issues