Fixed some subtle potentially lurking memory access bugs