polish the code, add missing docstrings