Include subquery refactor

This is work to improve the subquery logic to counter double joins etc.