Merged in Hai's code