Probably need to rethink the query algorithm