Minor bug fixes

Fixed a whole bunch of minor bugs
and CppCheck issues