More complex logic to generate necessary variables

We need quite a few temporary variables