Major refactoring and documentation update.