Fixed a number of warnings and some general tidy of up code