Small improvements to error detection, plus minor cleanup