Code cleanup: fixes based on findbugs 3.0 findings

Running findbugs 3.0 on the code base provided some useful
cleanup suggestions, and some direct bugs which should be fixed.
This commit fixes the most obvious ones, there are still some
findbugs findings left that should be investigated more.