Refactor command execution code