porting recent changes from master.