fix bugs, better way of generating tables