My fix for issue #966
Moved code to where it belongs.
Somehow it got left in the old place, so the bug was still occurring.
Added test for add_row() failures.