Changed Ruibin's code