Optimized id_pair() so that clumps of IDs get handled more