added documentation, some refactoring and cleanup