refactor common command and exec code