Import from https://falling-sky.googlecode.com/svn/trunk/source r1511