back porting some fixes from master