First porting attempt to mainline