improved Query::encode method