Fixed several small bugs found by static code analysis