Merged tianwei's code.