Fix some issues reported by Coverity scan

This is the same #14 updated after your comments.