moved query to elem & fixed instanceof check in wrapElement