Refactor: extract common patterns from the main commands