Updated Vinh's code.