varius fixes & cleanups, sync with upstream