Major rewrite of the tablegen function for less load. Thanks dywi