Code review

@bgchun and myself have been hunting for a bug today and did some small quality improvements on the code along the way.