Added documentation, fixed the case where mapping function produces the same key multiple times.