Migrated code from fog/fog#3225