many refactoring. add documentation. improve tests (fails)