Ned/improve pr 4347

@louyihua: I made the changes I suggested.