Cloned from rdpeng.