Fix some memory and intialisation issues