Refactor a bunch of table generation stuff.