The input logic is totally odd

No values exist for $X and $Y

It the Loop for the Write main box at the first EndIF that causes the problem. The branches are numbers.