major overhaul, switched to data.table, prepare to use dbWriteTable