#2 code from SVN core