Backport fixes for #342 and #343.