Initial version, before hacks from Ralph