Changed iteration order. Added counter dict. Added TODO comments