Merged Arif's and Yikang's code, bug fix