Move logic out of command into more readily tested function