Backport changes from the grammar branch