Bugfixes and refactoring
Doc update