Optimized the buildTable function.