Extracted BasicRef and fixed bug in selecting sub queries.