changes to query to support multilevel parenthesis