Change implementation of distinct to a more type-clear way.Add docstring