Rewrite parts of CLI to make testable