minor refactor and add docstrings