Break out execution of cmd into a function.
listen for and handle commands