ported from 4.x