Refactor commands to reduce code duplication