Ported from svn