FYI, this will be expensive for large datasets.