Initial refactoring to lib