Documentation and some refactoring