Various fixes suggested by IntelliJ IDEA's Analyze tool.