bug fixes and tidy up