moved query encode in own function