reorganized and better commented for pydoc