Refactoring the command handling