Refactoring a piece of the code and adding sphinx docs.