If each of these functions took a table as an argument, and were called in a loop with each table in turn, I think the code could be a lot simpler.