cleanup query code