Various fixes recommended by PVS Studio