used sphinx-style docstring and improved the code