added docs, some refactoring