Refactoring lib and tests