Add docstring, simplify code