some lib fixes and refactoring