merga code from cwlin.

merga code from cwlin.