modified Yu's code