refactor to allow for both api and cli code