documentation added and some refactoring