Refactor move core code to lib package