Fix everything flagged by initial coverity scan

Coverity picked up 14 defects, this fixes 13 of them (one of them should be false due to libuv usage but I may clean it up later anyway).