Polished the fix, ready for merge. See comments.