Added more documentation. Also some refactoring