Various fixes and tidy up of code.