minor bug fixes and tidy up