Merge part of jc/portable branch