added new changes from Xiao's code