Extracted function from _main()