Refactored table generation to avoid some of the duplicated code.