Merged Yan's and Christal's code