factored out writing of tables