first version of sub query handling