Major refactoring and adding documentation.