API cleanup, minor fixes, upstream backport.