Initial version of query algorithm.