updated code from yichen