Minor reorganisation and a few bug fixes.