Bug fixes and tidying up