More Coverity fixes

Fix three possible segfaults reported by Coverity, plus a cleanup found while reviewing code.