Cherry-picked fixes

Here's a bunch more fixes. This time they're mostly around file-writing and performance. I've created a special branch and cherry-picked the fixes you probably want and left behind the ones you probably don't (like iOS 5 targeting and header project/public settings).