A variety of changes to sync with the C version.