Fix bugs for wrong table and indexerror