Add docstrings
Refactor some functions
Update test with the changes
Solve runtime errors