Bug fixes and tidy up