Improved Query logic.