Lots of refactorizations and documentation.