Carried over useful changes from HTML branch.