improve error reporting, add primary key to stats table