merge Chao's codes