Refactored execute_cmd