more efficient implementation of collected_by?