Some refactoring and documentation