Fix for more efficient query