Tried yet another implementation, to no avail. This one seems worth saving, though.