factored out main