Refactor to separate library functions out better