Small bug fixes and tidy-ups