This series started as a few command-related bug fixes, but while doing this I saw that several refactorings were needed, and so I've added these in as well. Regression tested by the buildbot. Tom