Some refactoring and documentation.