Query refactory