Refactored QueryActivity, reducing its size.