Working version; all doctests pass