Integrated changes from Google Code master, and simplified a bit more too.