Porting #859 to master