ported from CarbonDev