Updated logic and docstring.