Add command retrying (also fix some bugs).