Misc fixes and upstream merge