Ported recent changes from 3.0 branch