changed reference to $table for function preparetable