Thanks for all the great advice. I have looked at tie (at last) and yes I got what I needed from that. I've messed around with Hugo's idea before and I agree its much easier to understand but in this instance tie works out right.
However I've found it very difficult to explain to my boss!