Mass refactoring and documentation