A lot of fixes null pointers etc