Refactoring, fixes and documentation.