Split implementation into lib and cli.