refactorization and docstrings