fixed query selection and state management issues