rewrote parts using collections.Counter to make it python 2.6 compatible.