refactor into lib