Refactor

refactor to be usable as a lib