Some internal reorganisation, small cleanup and fixes