merged Xinjie's lastest code with Devin's;