Documentation added, some refactoring