polished up a few things within write_sql method