Jwang refactor query encoding

I see, nice property! Done in 11eb254.