Code cleanup

Fixed several minor bugs discovered by FindBugs