Cleaned up and reduced complexity of the CommandHandler. See comments
for details.