Updated Query logic