Attempt to handle derived tables ; refactored subquery handling