Add #168, #224 from upstream and some patches we've made