fixed subquery handling