Jwang refactor query encoding

Got it, done in 11eb254.