fixed some NPEs and tidied up a bit