split commands into their own functions; improve logging and error reporting