Refactor command_factory and add exception handling.
Implement the ComposeCommand.