move command code into its own function