Incorporated fixes from upstream branch