Fix and a new implementation using collections.Counter