Improved documentation and some refactoring