Tidy things up and a few bug fixes