refactoring into library, work in progress