Manually cherry picking some useful code from some other forks that were never pulled back in to upstream.