I think you're getting the error because the parens aren't nested correctly. Here is one close-paren too many here, so the N1 loop below is a sibling instead of a child. The bigger problem is that you have one to *few* close-parens after the N1 loop, so Table 3 is accidentally a child of Table 2.