factor out command processing