refactory query